package com.ftwj.demo.modular.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import com.ftwj.demo.base.common.BaseDao;
import com.ftwj.demo.base.util.PageInfoBean;
import com.ftwj.demo.modular.entity.MessageBean;

/**
*
* <p>Title: MessageDao</p>  
* <p>Description: 描述</p>
* <p>Copyright: Copyright (c) 2020 </p>  
* <p>Company: 兰州飞天网景信息产业有限公司</p>    
* @author WangSli
* @date 2020年04月07日 14:23:51
* @version V1.0
*/
@Repository
public class MessageDao extends BaseDao<MessageBean, Integer> {

	/**
	 * <p>Title: page</p>  
	 * <p>Description: 分页查询</p>  
	 * @param pageInfo
	 * @author HeYang
	 * @date 2020年4月13日  
	 * @version 1.0
	 */
	public PageInfoBean page(PageInfoBean pageInfo,String title) {
		MessageBean bean = new MessageBean();
		bean.setTitle(title);
		return this.pageMapByExample(bean, pageInfo);
	}

	/**
	 * <p>Title: mySendPage</p>  
	 * <p>Description: 我发送的消息</p>  
	 * @param pageInfo
	 * @param title
	 * @param userId
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	public PageInfoBean mySendPage(PageInfoBean pageInfo, String title,Integer userId) {
		MessageBean bean = new MessageBean();
		bean.setTitle(title).setCreateUser(userId);
		return this.pageMapByExample(bean, pageInfo);
	}

	/**
	 * <p>Title: myRelatedPage</p>  
	 * <p>Description: 与我相关的消息分页列表</p>  
	 * @param pageInfo
	 * @param title
	 * @param userId
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	public PageInfoBean myRelatedPage(PageInfoBean pageInfo, String title,Integer userId) {
		String sql = "SELECT m.* FROM tb_message m,tb_message_user u WHERE m.mess_id = u.mess_id AND m.state = 1 AND u.state = 0 AND u.user_id = ?";
		List<Object> list = new ArrayList<Object>();
		list.add(userId);
		if(!StringUtils.isEmpty(title)) {
			sql += " AND m.title LIKE ?";
			list.add("%"+title+"%");
		}
		return jtl.page(sql, pageInfo, list.toArray());
	}

	/**
	 * <p>Title: getMyRelatedList</p>  
	 * <p>Description: 与我相关的消息列表</p>  
	 * @param title
	 * @param userId
	 * @author HeYang
	 * @date 2020年5月6日  
	 * @version 1.0
	 */
	public List<Map<String, Object>> getMyRelatedList(String title,Integer userId) {
		String sql = "SELECT * FROM tb_message m,tb_message_user u WHERE m.mess_id = u.mess_user_id AND u.user_id = ?";
		if(!StringUtils.isEmpty(title)) {
			sql += " AND m.title LIKE ?";
			return this.jtl.queryForList(sql, userId,"%"+title+"%");
		}else {
			return this.jtl.queryForList(sql, userId);
		}
	}

}
